<?php

/**
 * To change this template, choose Tools | Templates
 *  * @author Samy Deghou(deghou@polytech.unice.fr ; deghou@embl.de)
 * 		   Stephan Beisken (beisken@ebi.ac.uk)
 * 		   Luis Felipe De Figueiredo (ldpf@ebi.ac.uk)
 * and open the template in the editor.
 */

/**
 * Description of ConditionType
 *
 * @author ldpf
 */
class ConditionType {
    /*
     * Attributes
     */

    private $_conditionTypeID; //auto increment in the database
    private $_conditionName;
    private $_conditionType;
    private $_datatype;
    private $_units;

    /**
     * Constructor
     */
    public function ConditionType($conditionName, $conditionType, $datatype, $units, $conditionTypeID=-1) {
        $this->_conditionTypeID = $conditionTypeID;
        $this->_conditionName = $conditionName;
        $this->_conditionType = $conditionType;
        $this->_datatype = $datatype;
        $this->_units = $units;
    }

    /*
     * Accessors
     */

    public function get_conditionTypeID() {
        return $this->_conditionTypeID;
    }

    public function get_conditionName() {
        return $this->_conditionName;
    }

    public function get_conditionType() {
        return $this->_conditionType;
    }

    public function get_datatype() {
        return $this->_datatype;
    }

    public function get_units() {
        return $this->_units;
    }

    /*
     * Modifiers
     */

    public function set_conditionTypeID($_conditionTypeID) {
        $this->_conditionTypeID = $_conditionTypeID;
    }

    public function set_conditionName($_conditionName) {
        $this->_conditionName = $_conditionName;
    }

    public function set_conditionType($_conditionType) {
        $this->_conditionType = $_conditionType;
    }

    public function set_datatype($_datatype) {
        $this->_datatype = $_datatype;
    }

    public function set_units($_units) {
        $this->_units = $_units;
    }

    /**
     * create a new row in the database
     */
    public function create() {
        if ($this->_conditionTypeID != -1)
            die("Unable to create, still existing !");
        elseif ($this->existsEntry()) {// test if condition type exists
            Database::connect();
            $query = "INSERT INTO `CONDITION_TYPE` values (NULL,'" .
                    $this->_conditionName . "','" .
                    $this->_conditionType . "','" .
                    $this->_datatype . "','" .
                    $this->_units . "')";
            mysql_query($query) or die("ERROR NB " . mysql_errno() . " : " . mysql_error());
            $this->_idSujet = mysql_insert_id();
            Database::disconnect();
            return $this->_idSujet;
        } else {
            echo 'Entry already exists!';
            return -1;
        }
    }

    /**
     * Read an object from the database
     */
    public static function read($key) {

        Database::connect();
        $query = "SELECT * FROM CONDITION WHERE condition_type_id = '" . $key . "'";
        $ans = mysql_fetch_assoc(mysql_query($query));
        extract($ans);
        Database::disconnect();
        return new Condition_Type($condition_name, $condition_type, $data_type, $units, $condition_type_id);
    }

    /**
     * Check if the row already exists in the CONDITION_TYPE table
     * @return boolean
     */
    public function existsEntry() {
        Database::connect();
        $query = "SELECT COUNT (*) FROM `CONDITION_TYPE` WHERE condition_name='" .
                $this->_conditionName . "' AND condition_type='" .
                $this->_conditionType . "' AND data_type='" .
                $this->_datatype . "' AND '" .
                $this->_units . "')";
        Database::disconnect();
        return (mysql_query($query) > 0);
    }

    /**
     * get the key of an existing rown of CONDITION_TYPE table
     * @return int
     */
    public function getEntryKey() {
        Database::connect();
        $query = "SELECT COUNT (*) FROM `CONDITION_TYPE` WHERE condition_name='" .
                $this->_conditionName . "' AND condition_type='" .
                $this->_conditionType . "' AND data_type='" .
                $this->_datatype . "' AND '" .
                $this->_units . "')";
        $ans = mysql_fetch_assoc(mysql_query($query));
        extract($ans);
        Database::disconnect();
        return $conditionTypeID;
    }

}
?>
